var rootPath = getRootPath();

$(function() {

	$("#admin-help-save").click(function() {
		if(document.forms[0].rleName.value==""){
			 layer.alert("名称不能为空");		
		}else{
			var name = $('#name').val();
			$.ajax({ //获取模块信息
				url : rootPath + '/isnameexsit',
				type : 'POST',
				data:{
					"modulename":name,	
				},
				success : function(successdata) {	
				console.log(successdata)
				if (successdata.dataObject=="没有重复数据"){
					var formData = new FormData($("#helpAddForm")[0]); 		
					 formData.append("file",$("#logoFile")[0].files[0])
					 console.log(formData);		
					$.ajax({
						url:rootPath+"/uploadImg",
						type:"POST",
						data:formData,
						contentType:false,
						processData:false,
						async : false,
						success:function(data){
							console.log("进入"); 
							var name = $('#name').val();
							var productid = $('#productid').val();
						    var tucaourl = $('#tucaourl').val();
						    var sort=$("#help_select option:selected").val();
						    console.log(name); 
						    $.ajax({
								url:rootPath+"/addproduce",
								type:"POST",
								data:{
									"productid":productid,
									"name":name,						
									"tucaourl":tucaourl,
									"sort":sort,
									"imgurl":data						
								},	
								success:function(data){
									layer.msg('成功',{icon:1,time:1000},function() {
										parent.window.location.href = rootPath + '/helpPage';
										var index = parent.layer.getFrameIndex(window.name);
										parent.layer.close(index);
									});
									
								},error: function () {
									 layer.alert("添加失败");
						        }
								
							});
						    
						   
						},
						error: function () {
							 layer.alert("失败");
				        }
					
					});
				}else{
					 layer.alert("有重复名称");
				}
				
					
				},
				error : function() {
					layer.alert("获取数据失败");
				}
		});
			
		}
		
		

		
		
		
	
	})
})

function preview(file){
	console.log(file.files)
	$("#imgHidden").css("display", "none");
	var prevDiv = document.getElementById('preview');
	if(file.files && file.files[0]){
		var reader = new FileReader(); 
		reader.onload = function(evt) {
		prevDiv.innerHTML = '<img style="width: 100px;height: 100px;" src="' + evt.target.result + '" />';
	 }
		reader.readAsDataURL(file.files[0]);
	}else{
		prevDiv.innerHTML = '<div class="img" style="width: 100px;height:100px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + 
		file.value + '\'"></div>';
	}
}